﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace PhilWicklund.com.FacebookParts
{
    public class SharePart: System.Web.UI.WebControls.WebParts.WebPart
    {
        private string customUrl = "";
        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Custom URL"),
         System.Web.UI.WebControls.WebParts.WebDescription("Enter the that you want your end users to share on their facebook profiles."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue("")
        ]
        public string CustomUrl
        {
            get
            {
                return customUrl;
            }
            set
            {
                customUrl = value;
            }
        }

        private bool useLocalUrl = true;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Use Local URL"),
         System.Web.UI.WebControls.WebParts.WebDescription("If true, the URL of the page that the web part is placed on will be the URL that facebook users are shareing."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(true)
        ]
        public bool UseLocalUrl
        {
            get
            {
                return useLocalUrl;
            }
            set
            {
                useLocalUrl = value;
            }
        }

        private Orientations orientation = Orientations.left;
        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Orientation"),
         System.Web.UI.WebControls.WebParts.WebDescription("Select the centering for the web part."),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.User),
         System.ComponentModel.Category("Facebook Settings"),
         System.ComponentModel.DefaultValue(Orientations.left)
        ]
        public Orientations Orientation
        {
            get
            {
                return orientation;
            }
            set
            {
                orientation = value;
            }
        }

        protected override void CreateChildControls()
        {
            
            try
            {
                StringBuilder script = new StringBuilder();
                script.Append("<table width='100%'><tr><td align='" + Orientation.ToString() + "' width='100%'>");
                if (UseLocalUrl)
                    script.Append("<fb:share-button href=\"" + this.Page.Request.Url + "\"></fb:share-button>");
                else
                    script.Append("<fb:share-button href=\"" + CustomUrl + "\"></fb:share-button>");
                script.Append("</td></tr></table>");

                this.Controls.Add(new LiteralControl(script.ToString()));
            }
            catch (Exception e) { this.Controls.Add(new LiteralControl(e.Message)); }
        }
    }
}
